package org.one.stone.soup.db.engine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.one.stone.soup.db.indexing.GISDataIndex;
import org.one.stone.soup.db.indexing.KeyValueIndex;
import org.one.stone.soup.db.indexing.LocalToRemoteIndexMap;
import org.one.stone.soup.db.indexing.TimeDataIndex;
import org.one.stone.soup.db.processors.JSON;

public class GISDataEngine {
	
	private LocalToRemoteIndexMap dataStore;
	
	public long store(String record) throws IOException {
		JSON json = SoupDB.instance().getJavascriptEngine().toJSON( record );
		
		String name = json.getName();		
		
		long id = SoupDB.instance().store(name, record.getBytes());
		SoupDB.instance().index( id,json,GISDataIndex.class );
		SoupDB.instance().index( id,json,TimeDataIndex.class );
		SoupDB.instance().index( id,json,KeyValueIndex.class );
		
		return id;
	}
	
	public List<String> retrieveInArea(double latitudeA, double longitudeA, double radius) throws IOException {
		
		GISDataIndex gisIndex = SoupDB.instance().getIndex( GISDataIndex.class );
		
		Long[] indexes = gisIndex.findInArea(latitudeA, longitudeA, radius);
		List<String> results = new ArrayList<String>();
		for(int index=0;index<indexes.length;index++) {
			results.add( new String(dataStore.retrieve(indexes[index])) );
		}
		return results;
	}

	public List<String> retrieveInPeriod(String timeA, String timeB) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<String> retrieveInPeriodAndArea(String timeA, String timeB,
			double latitudeA, double longitudeA, double radius) {
		// TODO Auto-generated method stub
		return null;
	}
}
